package com.buaa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.dto.UserExtensionActivityDTO;
import com.buaa.entity.UserExtensionActivity;
import com.buaa.enums.ResponseCode;
import com.buaa.exception.ServiceException;
import com.buaa.mapper.UserExtensionActivtyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserExtensionActivityServiceImpl extends ServiceImpl<UserExtensionActivtyMapper, UserExtensionActivity> implements IService <UserExtensionActivity> {

    @Autowired
    private UserExtensionActivtyMapper userExtensionActivtyMapper;

    public List<UserExtensionActivity> getUserExtList() {
        List<UserExtensionActivity> extList = null;
        try {
            extList = list();
        }catch (ServiceException e){
            new ServiceException(ResponseCode.COMMON_SYS_ERROR.getKey(), ResponseCode.COMMON_SYS_ERROR.getDesc());
        }
        return extList;
    }

    // 默认不带参数分页查询
    public Map<String,Object> findPageByDefault2(Integer pageNum, Integer pageSize) {
        IPage<UserExtensionActivity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UserExtensionActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("revision_time")
                .last("LIMIT " + pageNum + "," + pageSize);
        long total = count(queryWrapper);
        //调用page方法，根据分页对象查询用户信息
        IPage<UserExtensionActivity> userPage = page(page, queryWrapper); // 调用 page 方法
        List<UserExtensionActivity> userList = userPage.getRecords();
        Map<String, Object> res = new HashMap<>();
        res.put("pagedata", userList);
        res.put("total", total);
        return res;
        // 返回查询结果中的用户记录列表
    }



    public Map<String,Object> findPageByPidAndUserName(String policeId, String userName,Integer pageNum, Integer pageSize){
        if (StrUtil.isBlank(userName) && StrUtil.isBlank(policeId))
            return findPageByDefault(pageNum, pageSize);
        if (StrUtil.isBlank(userName) && !StrUtil.isBlank(policeId)) {
            //policeId = "%" + policeId +"%"; // mybatis 中 "@select "不支持 %% 这种匹配
            return findPageByPoliceId(policeId, pageNum, pageSize);
        }
        if (!StrUtil.isBlank(userName) && StrUtil.isBlank(policeId)) {
//            userName = "%" + userName +"%"; // mybatis 中 "@select "不支持 %% 这种匹配
            return findPageByUserName(userName, pageNum, pageSize);
        }
        Map<String, Object> res = new HashMap<>();
        if (!StrUtil.isBlank(policeId) && !StrUtil.isBlank(userName)) {
            int total = userExtensionActivtyMapper.getTotal();
            pageNum = (pageNum - 1) * pageSize;
            List<UserExtensionActivityDTO> userextList = userExtensionActivtyMapper.getUserList(policeId, userName, pageNum, pageSize);
            res.put("pagedata", userextList);
            res.put("total", total);
        } else {
            new ServiceException(ResponseCode.QRY_FAILURE.getKey(), ResponseCode.QRY_FAILURE.getDesc()); // 查询失败
        }
        return res;
    }


    public Map<String,Object> findPageByDefault(Integer pageNum, Integer pageSize){
        int total = userExtensionActivtyMapper.getTotal();
        pageNum = (pageNum - 1) * pageSize;
        List<UserExtensionActivityDTO> userextList = userExtensionActivtyMapper.getUserListByDefault(pageNum, pageSize);
        Map<String,Object> res=new HashMap<>();
        res.put("pagedata",userextList);
        res.put("total",total);
        return res;
    }

    public Map<String,Object> findPageByPoliceId(String policeId, Integer pageNum, Integer pageSize){
        int total = 0;
        pageNum = (pageNum - 1) * pageSize;
        if (StrUtil.isBlank(policeId)) {
            total = userExtensionActivtyMapper.getTotal();
        }
        else {
            total = userExtensionActivtyMapper.getTotalWithPoliceId(policeId);
        }

        List<UserExtensionActivityDTO> userextList = userExtensionActivtyMapper.getUserListByPid(policeId, pageNum, pageSize);
        Map<String,Object> res=new HashMap<>();
        res.put("pagedata",userextList);
        res.put("total",total);
        return res;
    }

    public Map<String,Object> findPageByUserName(String userName, Integer pageNum, Integer pageSize){
        int total = 0;
        pageNum = (pageNum - 1) * pageSize;
        if (StrUtil.isBlank(userName)) {
            total = userExtensionActivtyMapper.getTotal();
        }
        else {
            total = userExtensionActivtyMapper.getTotalWithUserName(userName);
        }
        List<UserExtensionActivityDTO> userextList = userExtensionActivtyMapper.getUserListByUserName(userName, pageNum, pageSize);
        Map<String,Object> res=new HashMap<>();
        res.put("pagedata",userextList);
        res.put("total",total);
        return res;
    }

}


